package com.anjie.powerproject.strategy.utils;

import java.util.*;

public class ElectricityPriceUtil {
    // 电价字符串
    private String priceStr;
    // 间隔数组
    private int[] num;
    // 电价数组
    private double[] priceList;

    private Map<Integer, double[]> priceMap;

    public ElectricityPriceUtil(String priceStr) {
        this.priceStr = priceStr;
        // 求单位时间间隔
        this.num = Arrays.stream(this.priceStr.split(",")).mapToInt(item->item.split("\\+").length).toArray();
        this.priceList = this.createPriceList();
    }

    public int getPriceListSize() {
        return priceList.length;
    }

    public double[] createPriceList() {
        double[] priceList = new double[Arrays.stream(this.num).sum()];
        String[] priceSplit = this.priceStr.split(",");
        this.priceMap = new HashMap<>();

        for(int i=0; i<24; i++) {
            int count = this.num[i];
            String[] split = priceSplit[i] .split("\\+");
            assert count == split.length;

            int k = Arrays.stream(this.num).limit(i).sum();

            double[] temp = new double[count];

            for (int j=0; j<count; j++) {
                priceList[k + j] = Double.parseDouble(split[j]);

                temp[j] = Double.parseDouble(split[j]);
            }

            priceMap.put(i, temp);
        }
        return priceList;
    }

    // 获取单纯电价列表
    public double[] getPriceList() {
        return priceList;
    }

    // 获取（小时：电价）列表
    public Map<Integer, double[]> getPriceListWithClock() {
        return priceMap;
    }

    // 获取 峰/平/谷 价格
    public double[] minMiddleMaxPrice() {
        return Arrays.stream(this.priceList).distinct().sorted().toArray();
    }

    // 获取index对应pricelist中的时间
    public String getTimebyIndex(int index) {
        int sum = 0;
        for (int i=0; i<24; i++) {
            int count = this.num[i];
            if (index >= sum && index < sum + count) {
                int offset = index - sum;

                String hour = String.format("%02d", i);
                String minue = String.format("%02d", (int)(offset * 60 / count));
                return hour + ":" + minue;
            }
            sum += count;
        }

        if (sum == index) {
            return "00:00";
        }

        return "";
    }

    // 获取（电价：索引）列表
    public Map<Double, List<String>> getPriceListWithIndex() {
        double[] prices = minMiddleMaxPrice();

        Map<Double, List<String>> map = new HashMap<>();

        // 第一步
        for (double price: prices) {
            // 找到第一个price
            int start = 0;
            int end = 0;
            for (start=0; start<this.priceList.length; start++) {
                if (price == this.priceList[start]) {
                    break;
                }
            }
            for (int i=start+1; i<this.priceList.length; i++) {
                if (price == this.priceList[i] && price != this.priceList[i-1]) {
                    start = i;
                }
                if ((price != this.priceList[i] && price == this.priceList[i-1]) || (price == this.priceList[i] && i == this.priceList.length-1)) {
                    if (price != this.priceList[i]) {
                        end = i-1;
                    } else {
                        end = i;
                    }
                    String dateStr = start + "~" + end;
                    List<String> list = map.get(price);
                    if (list == null) {
                        list = new ArrayList<>();
                    }
                    list.add(dateStr);
                    map.put(price, list);
                }
            }

            // 检查首尾
            List<String> list = map.get(price);
            int startIndex = -1;
            int endIndex = -1;
            for(int i=0 ; i<list.size(); i++) {
                if (list.get(i).contains("0~")) {
                    startIndex = i;
                }
                if (list.get(i).contains("~" + (this.priceList.length-1))) {
                    endIndex = i;
                }
            }

            if (startIndex != endIndex && endIndex != -1 && startIndex != -1) {
                list.set(startIndex, list.get(startIndex).replaceFirst("^0", list.get(endIndex).split("~")[0]));
                list.remove(endIndex);
            }

        }

        return map;
    }

    // 获取（电价：时间）列表
    public Map<Double, List<String>> getPriceListWithTime() {
        double[] prices = minMiddleMaxPrice();

        Map<Double, List<String>> map = new HashMap<>();

        // 第一步
        for (double price: prices) {
            // 找到第一个price
            int start = 0;
            int end = 0;
            for (start=0; start<this.priceList.length; start++) {
                if (price == this.priceList[start]) {
                    break;
                }
            }
            for (int i=start+1; i<this.priceList.length; i++) {
                if (price == this.priceList[i] && price != this.priceList[i-1]) {
                    start = i;
                }
                if ((price != this.priceList[i] && price == this.priceList[i-1]) || (price == this.priceList[i] && i == this.priceList.length-1)) {
                    if (price != this.priceList[i]) {
                        end = i-1;
                    } else {
                        end = i;
                    }
                    String dateStr = start + "~" + end;
                    List<String> list = map.get(price);
                    if (list == null) {
                        list = new ArrayList<>();
                    }
                    list.add(dateStr);
                    map.put(price, list);
                }
            }

            // 检查首尾
            List<String> list = map.get(price);
            int startIndex = -1;
            int endIndex = -1;
            for(int i=0 ; i<list.size(); i++) {
                if (list.get(i).contains("0~")) {
                    startIndex = i;
                }
                if (list.get(i).contains("~" + (this.priceList.length-1))) {
                    endIndex = i;
                }
            }

            if (startIndex != endIndex && endIndex != -1 && startIndex != -1) {
                list.set(startIndex, list.get(startIndex).replaceFirst("^0", list.get(endIndex).split("~")[0]));
                list.remove(endIndex);
            }

            // 替换
            List<String> replace = map.get(price);
            for(int i=0 ; i<replace.size(); i++) {
                replace.set(i, getTimebyIndex(Integer.parseInt(replace.get(i).split("~")[0])) + "~" + getTimebyIndex(Integer.parseInt(replace.get(i).split("~")[1]) + 1));
            }

        }

        return map;
    }
}